﻿using B.S.RbacData.Domain;
using B.S.RbacData.ErrorCode;
using B.S.RbacData.Infrastructure;
using B.S.RbacData.Write.API.Applications.Commands.RBAC;
using MediatR;

namespace B.S.RbacData.Write.API.Applications.Commandhandlters.RBAC
{

    /// <summary>
    /// 逻辑删除角色命令处理
    /// </summary>
    public class IsDelRoleCommandHandlter : IRequestHandler<IsDelRoleCommand, ApiResult<int>>
    {
        private readonly IBaseRepository<RoleModel> roleRep;

        public IsDelRoleCommandHandlter(IBaseRepository<RoleModel> roleRep)
        {
            this.roleRep = roleRep;
        }

        public async Task<ApiResult<int>> Handle(IsDelRoleCommand request, CancellationToken cancellationToken)
        {

            //查询角色
            var role=roleRep.GetAll().FirstOrDefault(x=>x.ID==request.RoleId);
              
            if(role == null)
            {

                return new ApiResult<int>()
                {
                    code = ResultEnumCode.fails,
                    data = 0,
                    message = "要删除角色信息不存在"

                };

            }
            //逻辑删除角色
            role.IsDeleted=true;
            role.UpdateTime=DateTime.Now;
            await roleRep.UpdateAsync(role);


            return new ApiResult<int>()
            {
                code = ResultEnumCode.Ok,
                data = 1,
                message = "删除成功"

            };
        }
    }
}
